package com.shoujifeng.snowmusic.player;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.shoujifeng.snowmusic.player.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 我喜欢的歌曲
 * @author Administrator
 *
 */
public class MyLikeMusicActivity extends Activity{
	private ListView listView;
	private MyAdapter mAdapter;
	private View lineLay;
	private RelativeLayout headView;
	private TextView headText;
	private ImageButton breakBtn;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music_menu_content);
		init();
	}
	public void init() {
		listView = (ListView) findViewById(R.id.music_menu_list);
		listView.setDivider(null);
		lineLay = LayoutInflater.from(MyLikeMusicActivity.this).inflate(R.layout.line_layout, null);
		//listView.addFooterView(lineLay);
		headView = (RelativeLayout) findViewById(R.id.headLay);
		headText = (TextView) headView.findViewById(R.id.head_text);
		headText.setText("我喜欢的歌曲");
		breakBtn = (ImageButton) headView.findViewById(R.id.breakBtn);
		setOnClickListener();
		setListView();
	}

	private void setOnClickListener(){
		breakBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MyLikeMusicActivity.this,MyTabHost.class);
				startActivity(intent);
				overridePendingTransition(R.anim.bottom_in,android.R.anim.fade_out);
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				
				int i=0;
				System.out.println(i);
				
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
		{
			finish();
			
			overridePendingTransition(R.anim.no_move, R.anim.right_out);
		}
			break;

		default:
			break;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	private void setListView() {
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < 10; i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("playerName", "周杰伦");
			map.put("musicName", "菊花台");
			list.add(map);
		}
		mAdapter = new MyAdapter(this, list, R.layout.download_over_list,
				new String[] { "playerName","musicName" }, new int[] { R.id.playerName ,R.id.musicName});
		listView.setAdapter(mAdapter);
	}
	
	private class MyAdapter extends SimpleAdapter {
		int count = 0;
		private List<HashMap<String, String>> mItemList;

		public MyAdapter(Context context,
				List<HashMap<String, String>> arrayList, int songMenuList,
				String[] strings, int[] is) {
			super(context, arrayList, songMenuList, strings, is);
			mItemList = arrayList;
			if (arrayList == null) {
				count = 0;
			} else {
				count = arrayList.size();
			}
			// TODO Auto-generated constructor stub
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mItemList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mItemList.size();
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			HashMap<String, String> map = mItemList.get(position);
			final String musicName = (String) map.get("playerName");
			final String playerName = (String) map.get("musicName");
			View view = super.getView(position, convertView, parent);
			TextView musicNameText = (TextView) view.findViewById(R.id.musicName);
			TextView playerNameText = (TextView) view.findViewById(R.id.playerName);
			musicNameText.setText(musicName);
			playerNameText.setText(playerName);
			
			final LinearLayout moreLay = (LinearLayout) view
					.findViewById(R.id.music_more_lay);
			final ImageView imageView = (ImageView) view.findViewById(R.id.moreImg);
			imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					moreLay.setVisibility(View.VISIBLE);
				}
			});


			return view;
		}
	
	}
}
